#include "playscene.h"
#include"mypushbutton.h"
#include"dataconfig.h"
#include<QSoundEffect>
#include<QPropertyAnimation>
#include<QTimer>
#include<QMenuBar>
#include<QMenu>
#include<QPainter>
#include<QDebug>
#include<QLabel>
#include<QPalette>
#include"mycoin.h"
/*PlayScene::PlayScene(QWidget *parent)
    : QMainWindow{parent}
{}*/

PlayScene::PlayScene(int levelNum)
{
    QString str=QString("进入了第 %1 关").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;

    //初始化游戏场景
    //设置固定大小
    setFixedSize(420,688);

    //设置图标
    setWindowIcon(QIcon(":/res/new20240507154444.png"));

    //设置标题
    setWindowTitle("游戏场景");

    //创建菜单栏
    QMenuBar*bar=menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu*startMenu= bar->addMenu("开始");

    //创建退出菜单项
    QAction*quitAction= startMenu->addAction("退出");

    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回关卡按钮的音效
    QSoundEffect*backSound1=new QSoundEffect;
    backSound1->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));
    //翻金币按钮的音效
    QSoundEffect*GoldflipSound=new QSoundEffect;
    GoldflipSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
    //胜利按钮的音效
    QSoundEffect*winSound=new QSoundEffect;
    winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));
    //失败按钮的音效
    QSoundEffect*loseSound=new QSoundEffect;
    loseSound->setSource(QUrl::fromLocalFile(":/res/aigeicom.wav"));


    //返回按钮
    MyPushButton*backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){
        //告诉主场景 我返回了，主场景监听SelectLevelScene的返回按钮
        qDebug()<<"fanjinbi点击了返回按钮";

        //返回关卡按钮的音效
        backSound1->play();

        QTimer::singleShot(500,this,[=](){
                emit this->SelectSceneBack();
            });

    });

    //失败图片显示
    QLabel*loseLabel=new QLabel;
    QPixmap tmpPix2;
    tmpPix2.load(":/res/failure20240511102930.png");
    tmpPix2=tmpPix2.scaled(tmpPix2.width()/2,tmpPix2.height()/2);
    loseLabel->setGeometry(0,0,tmpPix2.width()/1,tmpPix2.height()/1);
    loseLabel->setPixmap(tmpPix2);
    loseLabel->setParent(this);
    loseLabel->move(this->width()-tmpPix2.width()*0.5,1-tmpPix2.height());

    //设立关于倒计时的定时器
    ti2=new QTimer(this);
    ti2->start(1000);

    time2=10;
    QLabel*timeCountdown=new QLabel;
    timeCountdown->setParent(this);


    //设置倒计时字体与颜色
    QString temp=QString::number(time2);
    QLabel *myLabel = new QLabel(temp);
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    myLabel->setPalette(pe);
    myLabel->setParent(this);

    //显示当前的关卡数
    QLabel*Label=new QLabel;
    Label->setParent(this);
    //设置字体
    QFont font;
    QFont font1;
    font.setFamily("华文隶书");
    font.setPointSize(30);
    font1.setFamily("华文隶书");
    font1.setPointSize(25);
    QString str1=QString("Level: %1").arg(this->levelIndex);
    //将字体设置到标签控件中
    Label->setFont(font1);
    Label->setText(str1);
    Label->setGeometry(10,this->height()-60,120,50);

    myLabel->setFont(font);
    myLabel->setGeometry(170,this->height()-130,120,50);

    //倒计时功能与弹出失败动画
    connect(ti2,&QTimer::timeout,[=](){
        if(this->isWin==false&&time2>0)
        {
            QString temp1=QString::number(time2);
            myLabel->setText(temp1);
            time2--;
        }
        else
        {if(this->isWin==false&&time2==0)
            {
                myLabel->setGeometry(140,this->height()-580,120,50);
                myLabel->setText("太菜了！");
                //时间到，弹出失败画面
                //失败音效
                loseSound->play();

                //移动失败的图片
                QPropertyAnimation*animation=new QPropertyAnimation(loseLabel,"geometry");
                //设置时间间隔
                animation->setDuration(1000);
                //设置开始位置
                animation->setStartValue(QRect(loseLabel->x()-210,loseLabel->y()+10,loseLabel->width(),loseLabel->height()));
                //设置结束位置
                animation->setEndValue(QRect(loseLabel->x()-90,loseLabel->y()+204,loseLabel->width(),loseLabel->height()));
                //设置缓和曲线
                animation->setEasingCurve(QEasingCurve::InOutBounce);
                //执行动画
                animation->start();
            }
        }
    });



    DataConfig config;
    //初始化每个关卡的二维数组
    for(int i=0;i<5;i++)
    {
        for (int j=0;j<5;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel*winLabel=new QLabel;
    QPixmap tmpPix1;
    tmpPix1.load(":/res/20240507173131.png");
    tmpPix1=tmpPix1.scaled(tmpPix1.width()/2,tmpPix1.height()/2);
    winLabel->setGeometry(0,0,tmpPix1.width()/1,tmpPix1.height()/1);
    winLabel->setPixmap(tmpPix1);
    winLabel->setParent(this);
    winLabel->move(this->width()-tmpPix1.width()*0.5,1-tmpPix1.height());




    //显示金币背景图案
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            //绘制背景图片
            QPixmap pix=QPixmap(":/res/BoardNode(1).png");
            QLabel*labelH=new QLabel;
            labelH->setGeometry(20,20,pix.width(),pix.height());
            labelH->setPixmap(pix);
            labelH->setParent(this);
            labelH->move(97+i*50,200+j*50);


            //创建金币
            QString str;
            if(this->gameArray[i][j]==1)
            {
                //显示金币
                str=":/res/Coin0001.png";
            }else{
                //显示银币
                str=":/res/Coin0008.png";
            }

            //实例化coin对象
            MyCoin*coin=new MyCoin(str);
            coin->setParent(this);
            coin->move(97+i*50,203+j*50);

            //给金币属性赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];//1为正面，0为反面

            //将金币放入到金币的二维数组中，以便于后期的维护
            coinBtn[i][j]=coin;


            //点击金币 进行翻转
            connect(coin,&MyCoin::clicked,[=](){


                qDebug()<<"点击了金币！";
                coin->changeFlag();
                this->gameArray[i][j]=(this->gameArray[i][j]==0?1:0);

                //翻金币音效
                GoldflipSound->play();

                //延时翻转周围硬币
                QTimer::singleShot(300,this,[=](){

                    //周围右侧金币反转条件
                    if(coin->posX+1<=4)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                        qDebug()<<"反转了右";
                    }
                    //周围左侧硬币翻转条件
                    if(coin->posX-1>=0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                        qDebug()<<"反转了左";
                    }
                    //周围下侧硬币翻转条件
                    if(coin->posY+1<=4)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                        qDebug()<<"反转了下";
                    }
                    //周围上侧硬币翻转条件
                    if(coin->posY-1>=0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                        qDebug()<<"反转了上";
                    }


                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<5;i++)
                    {
                        for(int j=0;j<5;j++)
                        {
                            if(coinBtn[i][j]->flag==false)//只要有一个是反面，那就是失败
                            {
                                qDebug()<<"存在银币！";
                                this->isWin=false;
                                qDebug()<<isWin;
                                break;
                            }else{
                                qDebug()<<"金币！"<<isWin;}
                        }
                    }
                    if(this->isWin==true&&time2>0)
                    {
                        //胜利
                        qDebug()<<isWin;
                        //将所有按钮的胜利标志改为true
                        for(int i=0;i<5;i++)
                        {
                            for(int j=0;j<5;j++)
                            {
                                coinBtn[i][j]->isWin=true;
                            }
                        }


                        //胜利音效
                        winSound->play();

                        //移动胜利的图片
                        QPropertyAnimation*animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x()-210,winLabel->y()+10,winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x()-210,winLabel->y()+194,winLabel->width(),winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //执行动画
                        animation->start();
                    }


                });

            });
        }
    }
}


void PlayScene::timeOutThing()
{
    qDebug()<<"ajnja执行了该函数！";
    //设置倒计时字体与颜色
    QLabel *myLabel = new QLabel;
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    myLabel->setPalette(pe);
    myLabel->setParent(this);
    //设置字体
    qDebug()<<time1;
    QFont font;
    font.setFamily("华文隶书");
    font.setPointSize(20);
    myLabel->setFont(font);
    myLabel->setGeometry(110,this->height()-580,120,50);
    //显示倒计时
    if(time1>0)
    {
        QString temp=QString::number(time1);
        myLabel->setText(temp);
        time1--;
    }
    else
    {
        myLabel->setText("太菜了！");
    }


}


void PlayScene::paintEvent(QPaintEvent*)
{
    //创建背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/20240507154901.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
     pix.load(":/res/20240507154444.png");
     pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
     painter.drawPixmap(10,30,pix.width(),pix.height(),pix);
}
